package com.guo.xiao.bing.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guo.xiao.bing.common.Config;
import com.guo.xiao.bing.dao.NoteMapper;
import com.guo.xiao.bing.entity.Note;
import com.guo.xiao.bing.entity.NoteExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NoteService {

    @Autowired
    NoteMapper noteMapper;

    public PageInfo<Note> find(Integer currentPage) {

        NoteExample ne = new NoteExample();
        ne.setOrderByClause("id desc");

        PageHelper.startPage(currentPage, Config.PAGE_SIZE);
        List<Note> users = noteMapper.selectByExampleWithBLOBs(ne);
        PageInfo<Note> pageInfo = new PageInfo<>(users);

        return pageInfo;
    }

    public Note findById(Integer id) {
        return noteMapper.selectByPrimaryKey(id);
    }

    public Integer save(Note news) {
        return noteMapper.insert(news);
    }

    public Integer update(Note news) {
        return noteMapper.updateByPrimaryKeySelective(news);
    }

    public Integer delete(Integer id) {
        return noteMapper.deleteByPrimaryKey(id);
    }
}
